package com.hafu.view.kehu;
import java.awt.event.*;
import com.hafu.model.KhInfo;
import com.hafu.service.KhInfoService;
import com.hafu.util.StringUtil;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;
public class ManageKeHuFrm extends JFrame {
    private KhInfoService khInfoService = new KhInfoService();
    private int chooseId;
    public ManageKeHuFrm() {
        initComponents();
        fillTable();
    }
    private void fillTable() {
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        ArrayList<KhInfo> khInfoList = khInfoService.getKhInfoByName(new KhInfo());
        for (KhInfo khInfo : khInfoList) {
            Object[] rowData = {khInfo.getId(), khInfo.getName(), khInfo.getTel()};
            dtm.addRow(rowData);
        }
    }
    private void button1(ActionEvent e) {
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        String name = textField1.getText();
        KhInfo khInfo = new KhInfo();
        khInfo.setName(name);
        ArrayList<KhInfo> khInfoByName = khInfoService.getKhInfoByName(khInfo);
        if (khInfoByName.size() > 0) {
            for (KhInfo khInfo1 : khInfoByName) {
                Object[] rowData = {khInfo1.getId(), khInfo1.getName(), khInfo1.getTel()};
                dtm.addRow(rowData);
            }
        } else {
            JOptionPane.showMessageDialog(null, "没有找到客户信息");
        }
    }
    private void table1MouseClicked(MouseEvent e) {
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        chooseId = (int) dtm.getValueAt(row, 0);
        String name = (String)dtm.getValueAt(row, 1);
        String tel = (String)dtm.getValueAt(row, 2);
        textField2.setText(name);
        textField3.setText(tel);
    }
    private void button2(ActionEvent e) {
        String name = textField2.getText();
        String tel = textField3.getText();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int row = table1.getSelectedRow();

        // 检查是否有选中的行
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请先选择要修改的行");
            return;
        }

        // 获取ID并做类型检查
        Object idObj = dtm.getValueAt(row, 0);
        if (!(idObj instanceof Integer)) {
            JOptionPane.showMessageDialog(null, "无效的ID格式");
            return;
        }
        int id = (Integer) idObj;

        // 检查输入框内容是否为空
        if (StringUtil.isEmpty(name) || StringUtil.isEmpty(tel)) {
            JOptionPane.showMessageDialog(null, "客户名称和电话均不能为空");
            return;
        }

        KhInfo khInfo = new KhInfo(id, name, tel);
        try {
            Boolean updateKhInfo = khInfoService.updateKhInfo(khInfo);
            if (updateKhInfo) {
                JOptionPane.showMessageDialog(null, "修改成功");
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(null, "修改失败");
            }
        } catch (Exception ex) {
            // 记录日志或采取其他措施
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "发生错误：" + ex.getMessage());
        }
    }
    private void button3(ActionEvent e) {
        int row = table1.getSelectedRow();

        // 检查是否有选中的行
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请先选择要删除的行");
            return;
        }

        // 获取ID并做类型检查
        Object idObj = ((DefaultTableModel) table1.getModel()).getValueAt(row, 0);
        if (!(idObj instanceof Integer)) {
            JOptionPane.showMessageDialog(null, "无效的ID格式");
            return;
        }
        int id = (Integer) idObj;

        // 确认删除操作
        int confirmResult = JOptionPane.showConfirmDialog(null, "确定要删除该客户信息吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (confirmResult != JOptionPane.YES_OPTION) {
            return;
        }

        try {
            Boolean deleteKhInfo = khInfoService.deleteKhInfo(id);
            if (deleteKhInfo) {
                JOptionPane.showMessageDialog(null, "删除成功");
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(null, "删除失败");
            }
        } catch (Exception ex) {
            // 记录日志或采取其他措施
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "发生错误：" + ex.getMessage());
        }
    }

    private void initComponents() {
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        label3 = new JLabel();
        textField2 = new JTextField();
        textField3 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();
        setTitle("\u7ba1\u7406\u5ba2\u6237\u8868\u5355");
        Container contentPane = getContentPane();
        label1.setText("\u5ba2\u6237\u540d\u5b57");
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));
        {
            table1.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null, null},
                            {null, null, null},
                    },
                    new String[] {
                            "\u7f16\u53f7", "\u540d\u5b57", "\u7535\u8bdd"
                    }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }
        label2.setText("\u5ba2\u6237\u540d\u5b57");
        label3.setText("\u5ba2\u6237\u7535\u8bdd");
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> {
            button2(e);
            button2(e);
        });
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> button3(e));
        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(41, 41, 41)
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 611, GroupLayout.PREFERRED_SIZE)
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addComponent(label1, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(35, 35, 35)
                                                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 174, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(18, 18, 18)
                                                                .addComponent(button1, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE))))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(54, 54, 54)
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addComponent(label2)
                                                        .addComponent(label3))
                                                .addGap(56, 56, 56)
                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(textField2, GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                                                        .addComponent(textField3, GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))
                                                .addGap(194, 194, 194)
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addComponent(button2)
                                                        .addComponent(button3))))
                                .addContainerGap(156, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(35, 35, 35)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(button1)
                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label1))
                                .addGap(28, 28, 28)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 262, GroupLayout.PREFERRED_SIZE)
                                .addGap(34, 34, 34)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label2)
                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button2))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label3)
                                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button3))
                                .addGap(30, 30, 30))
        );
        pack();
        setLocationRelativeTo(getOwner());
    }
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField2;
    private JTextField textField3;
    private JButton button2;
    private JButton button3;
}
